#! /usr/bin/env python
'''
Created on Mar 30, 2011

@author: victor
'''
import os
import os.path
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import gameconf
import guiutils

import tkcolortable
from PyQt4 import QtGui

from UnitGraphicsItem import *

class MainWindow(QMainWindow):
    
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowIcon(QtGui.QIcon(guiutils.greator_icon()))
        self.createActions()
        self.createMenus()
        self.mapImage = QImage()
        self.scene = QGraphicsScene(self)
        self.scene.setSceneRect(0, 0, 400, 266)
        self.view = QGraphicsView()
        self.view.setScene(self.scene)
        self.setCentralWidget(self.view)
        
    def createMenus(self):
        self.fileMenu = self.menuBar().addMenu("&File")
        self.fileMenu.addAction(self.openAct)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction(self.exitAct)
        
    def createActions(self):
        self.openAct = QAction("&Open...", 
                               self,
                               shortcut=QKeySequence.Open,
                               statusTip="Open an existing game", 
                               triggered=self.openGame)
        self.openAct.curDir = "."
        
        self.exitAct = QAction("E&xit", 
                               self, 
                               shortcut="Ctrl+Q",
                               statusTip="Exit", 
                               triggered=self.close)
    
    def openGame(self):
        fileName = str(QFileDialog.getOpenFileName(
                        self,
                        "Open game",
                        self.openAct.curDir,
                        "Game Files (*.zip);;All Files (*)"))
        if fileName:
            self.loadGame(fileName)
                
    def loadGame(self, filename):
        self.openAct.curDir = os.path.dirname(filename)
        conf, errors = gameconf.load_gameconf_from_archive(filename)
        if len(errors) > 0:
            sys.stderr.write("\n".join(errors))
        else:
            self.mapImage.loadFromData(conf.get_map_img_data())
            self.scene.setSceneRect(0, 0, self.mapImage.width(), self.mapImage.height())
            self.scene.addPixmap(QPixmap.fromImage(self.mapImage))
            
            for u in conf.units.values():
                img = QImage()
                img.loadFromData(conf.get_unit_bitmap_data(u))
                rgbt = tkcolortable.ColorConverter.rgbt_color(u.bgcolour) 
                c0 = QColor(rgbt[0], rgbt[1], rgbt[2])
                rgbt = tkcolortable.ColorConverter.rgbt_color(u.colour) 
                c1 = QColor(rgbt[0], rgbt[1], rgbt[2])
                img.setColor(0, c0.rgba())
                img.setColor(1, c1.rgba())
                u_item = UnitGraphicsItem(QPixmap.fromImage(img), u, scene=self.scene)

if __name__ == '__main__':
    sys.stderr.write("Use ../runwa3r.pyw to start wa3r.\n")